import 'package:FineArt/constants.dart';
import 'package:FineArt/core/api/home_api.dart';
import 'package:FineArt/core/api/module_api.dart';
import 'package:FineArt/core/widget/web_view_page.dart';
import 'package:FineArt/core/widget/web_view_page_new.dart';
import 'package:FineArt/page/home/book_content_grid_page.dart';
import 'package:FineArt/page/imageview/ImagePreview.dart';
import 'package:FineArt/page/imageview/RecommendImagePreview.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:FineArt/page/imageview/imageview.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

class RecommendItem extends StatelessWidget {
  //
  final String title;
  final String type;
  final List<Object> list;

  VoidCallback moreClick;

  RecommendItem({Key key, this.title, this.type, this.list, this.moreClick})
      : super(key: key);

  List<Widget> buildRow(context) {
    List<Widget> l = new List();
    if (list == null) return l;
    if (list.isEmpty) return l;

    for (int i=0;i<list.length;i++) {
      Object object = list[i];
      //var types = ["works", "techart", "tutor"];
      String url = "";
      String info = "";
      String link = "";
      String avatar = "";
      String medium = "";
      String large = "";
      Image avartImg = null;
      int resid = 0;
      int restype = 1;
      String resurl = "";
      Resource res;
      if(type== "module"){
        res = object as Resource;
        url = res.url;
        restype = res.type;
      }
      // if (type == "works") {
      //   Works works = object as Works;
      //   url = works.thumburl;
      //   medium = works.sdurl;
      //   large = works.url + works.ext;
      //   info = works.title + "/" + works.nickname;
      //   avatar = works.portrait;
      //   resid = works.resid;
      //   restype = works.type;
      //   resurl = works.url;
      // } else if (type == "techart") {
      //   Techart techart = object as Techart;
      //   info = techart.title.isNotEmpty
      //       ? (techart.title + "/" + techart.nickname)
      //       : techart.nickname;
      //   url = techart.thumburl;
      //   medium = techart.sdurl;
      //   large = techart.url + techart.ext;
      //   avatar = techart.portrait;
      //   resid = techart.resid;
      //   restype = techart.type;
      //   resurl = techart.url;
      // } else if (type == "tutor") {
      //   Tutor tutor = object as Tutor;
      //   url = tutor.url;
      //   info = tutor.nickname;
      //   link = tutor.reflink;
      // }

      if (avatar == null || avatar.isEmpty) {
        avartImg = Image.asset(
          "assets/images/icon_header_default.png",
          width: getPortraitSWidth(21),
          height: getPortraitSHeight(21),
        );
      } else {
        avartImg = Image(
            width: getPortraitSWidth(21),
            height: getPortraitSHeight(21),
            image: CachedNetworkImageProvider(
              avatar,
            ));
      }

      Stack stack = new Stack(
        alignment: Alignment.center,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(radius(2)),
            child: Image(
                fit: BoxFit.cover,
                width: getPortraitSWidth(108),
                height: getPortraitSHeight(108),
                image: CachedNetworkImageProvider(
                  url,
                )),
          ),
          // Positioned(
          //     bottom: getPortraitSHeight(5),
          //     child: Container(
          //       alignment: Alignment.center,
          //       padding: EdgeInsets.symmetric(
          //           vertical: 2, horizontal: getPortraitSWidth(8)),
          //       decoration: BoxDecoration(
          //           borderRadius: BorderRadius.circular(radius(20)),
          //           color: Color.fromARGB(100, 0, 0, 0)),
          //       child: Row(
          //         crossAxisAlignment: CrossAxisAlignment.center,
          //         children: [
          //           avartImg,
          //           SizedBox(
          //             width: getPortraitSWidth(0),
          //           ),
          //           Text(
          //             info,
          //             style:
          //                 TextStyle(color: Colors.white, fontSize: setSp(10)),
          //           )
          //         ],
          //       ),
          //     ))
        ],
      );

      //需要InkWell包一下，否则stack无onTap接口
      InkWell well = new InkWell(
        child: stack,
        onTap: () {
          if (type == "tutor") {
            Navigator.of(context).push(MaterialPageRoute(builder: (context) {
              return WebViewPageNew(link, "");
            }));
          } else {
            // Resource resource = Resource(id: resid,type: restype, url: resurl,title: info);
            // ImageViewPage.JumpToImagePreview(resource, context);

            // RecommendResource resource = new RecommendResource(
            //     url: resurl,
            //     resid: resid,
            //     type: restype,
            //     sdurl: medium,
            //     hdurl: medium);
            // List<RecommendResource> resources = new List();
            // resources.add(resource);
            // RecommendImagePreview.JumpToPreview(0, 0, resources, context);

            if (restype == 1) //图片
                {
              ImagePreview.JumpToPreview(0, res.id.toString(),
                  "", i, list, context);
            } else if (restype == 0) {
              // BookPreview.JumpToPreview(resource, context, false);
              BookContentListPage.JumpToBookContentList(res, context, false);
            }
          }
        },
      );
      l.add(well);
    }

    return l;
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    return Container(
      margin:
          EdgeInsets.symmetric(vertical: 5, horizontal: getPortraitSWidth(8)),
      width: double.infinity,
      child: Column(
        children: [
          Row(children: [
            Text(
              this.title,
              style: TextStyle(
                  fontSize: setSp(17),
                  fontWeight: FontWeight.bold,
                  color: text_black),
            ),
            Expanded(
              child: Text(''), // 中间用Expanded控件
            ),
            InkWell(
              child: Text("更多",
                  style: TextStyle(fontSize: setSp(13), color: text_black)),
              onTap: moreClick,
            )
          ]),
          Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: buildRow(context)),
        ],
      ),
      // Here i can use size.width but use double.infinity because both work as a same
    );
  }
}
